﻿using System;
using System.Reflection;
using System.Windows.Forms;

namespace NuSoft.SistemaGestionPersonal.WinFormsUI
{
    public partial class MainForm : Form
    {
        private static FieldInfo _CloseReasonFormField = typeof(Form).GetField("closeReason", BindingFlags.Instance | BindingFlags.NonPublic);

        private EmployeesForm _employeesForm;
        private RestrictedAreasForm _restrictedAreasForm;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = MessageBox.Show(this, "¿Está seguro que desea salir?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No;
            }
        }
        
        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void EmployeesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_employeesForm == null)
            {
                _employeesForm = new EmployeesForm(MdiChildren_FormClosing);
                _employeesForm.MdiParent = this;
                _employeesForm.RefreshEmployees();
                _employeesForm.Show();
            }
            else
            {
                _employeesForm.RefreshEmployees();
                _CloseReasonFormField.SetValue(_employeesForm, CloseReason.None);
                _employeesForm.Show();
            }

            EmployeesToolStripMenuItem.Enabled = false;
            WindowToolStripMenuItem.Visible = true;
        }

        private void RestrictedAreasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_restrictedAreasForm == null)
            {
                _restrictedAreasForm = new RestrictedAreasForm(MdiChildren_FormClosing);
                _restrictedAreasForm.MdiParent = this;
                _restrictedAreasForm.RefreshRestrictedAreas();
                _restrictedAreasForm.Show();
            }
            else
            {
                _restrictedAreasForm.RefreshRestrictedAreas();
                _CloseReasonFormField.SetValue(_restrictedAreasForm, CloseReason.None);
                _restrictedAreasForm.Show();
            }

            RestrictedAreasToolStripMenuItem.Enabled = false;
            WindowToolStripMenuItem.Visible = true;
        }

        private void MdiChildren_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                ((Form)sender).Visible = false;
            }

            if (sender == _employeesForm)
            {
                EmployeesToolStripMenuItem.Enabled = true;
            }

            if (sender == _restrictedAreasForm)
            {
                RestrictedAreasToolStripMenuItem.Enabled = true;
            }

            if ((_employeesForm == null && _restrictedAreasForm == null) || ((_employeesForm == null || (_employeesForm != null && !_employeesForm.Visible)) && (_restrictedAreasForm == null || (_restrictedAreasForm != null && !_restrictedAreasForm.Visible))))
            {
                WindowToolStripMenuItem.Visible = false;
            }
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void HorizontalTileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void VerticalTileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }
    }
}